package com.lagou.servlet;

import com.lagou.bena.Student;
import com.lagou.service.AdminServiceImp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet(name = "queryStuServlet", urlPatterns = "/queryStu")
public class queryStuServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取到前端输入框中值
        request.setCharacterEncoding("utf-8");
        String info = request.getParameter("info");
        String stuName = request.getParameter("stuName");
        String email = request.getParameter("email");

        // 用来接收相关属性值，并set到student对象对应的成员变量中
        Student student = new Student();
        student.setInfo(info);
        student.setStuName(stuName);
        student.setEmail(email);

        AdminServiceImp asi = new AdminServiceImp();
        List<Student> students = null;
        try {
            students = asi.queryStuService(student);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        // 将查询出的所有学生信息集合存入session对象中实现多个请求共享
        request.getSession().setAttribute("students", students);
        // 实现客户端的跳转
        //response.sendRedirect("stuList.jsp");
        request.getRequestDispatcher("stuList.jsp").forward(request, response);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
